import moment from 'moment';
import _ from 'lodash';

// 禁用日期
export const disabledDate = (current, date) => {
  return current && current < moment().subtract(1, 'days');
};

// 判断值是否为非空值
export const isNotEmpty = (value) => {
  return value !== undefined && value !== null && value !== '';
};
export const CODE1 = '_title';
export const getTableData = (_data) => {
  const { data, head } = _data || {};
  if (!_data || !data || !head)
    return {
      tableData: [],
      exportTable: [],
    };
  // 数据处理
  const fmtRow = (_row, _title) => {
    let res = {
      id: _.uniqueId(),
      _title: _title,
    };
    head.map((k, i) => {
      let val = _row[i];
      val = parseFloat(val).toFixed(3);
      res[k] = val;
    });
    return res;
  };
  // 递归种一颗树
  let exportTable = []; // 导出用
  const fn = ({ total, ...list }, title, lv) => {
    let result = fmtRow(total, title);
    exportTable.push(result);
    let children = [];
    for (const k in list) {
      const temp = !!list[k]?.length ? fmtRow(list[k], k) : fn(list[k], k, lv + 1);
      children.push(temp);
      exportTable.push(temp);
    }
    if (!!children.length) {
      result.children = children.sort((a, b) => {
        return a[CODE1] - b[CODE1];
      });
    }
    return result;
  };
  // 总合计要单独一行
  const { children, ...total } = fn(data, '合计', 0);
  const tableData = [total, ...(children || [])];
  return { tableData, exportTable };
};
